package com.nb.hdj.baseClass;

import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;
import java.util.NoSuchElementException;

/**
 * @Description: Selenium基础类
 * @Author: hedongjia
 * @Date: 2021/11/3 下午5:35
 * @Version: v1.0
 */

public class SeleniumBase {

    protected WebDriver driver;
    private static final int elementWaitTime = 30;


    /**
     * 元素等待处理
     * @param driver
     * @param elementBy
     * @param handle
     * @return
     * @throws InterruptedException
     */
    public boolean elementWaitHandle(WebDriver driver, By elementBy, SeleniumUtilInterface handle) throws InterruptedException {
        try {
            WebDriverWait wait = new WebDriverWait(driver, elementWaitTime);
            WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(elementBy));
            //wait.until(ExpectedConditions.visibilityOfElementLocated(elementBy));
            if (handle != null) {
                handle.action(element);
            }
            return true;
        } catch (NoSuchElementException e) {
            e.fillInStackTrace();
            return false;
        } catch (TimeoutException e) {
            e.fillInStackTrace();
            return false;
        } catch (NullPointerException e) {
            e.fillInStackTrace();
            return false;
        }
    }

    /**
     * 找到可视元素后直接执行,也许元素被蒙层遮住,或者loading遮住会造成无法点击 调用visibilityOfElementLocated可解决此问题
     * @param driver
     * @param elementBy
     * @param handle
     * @return 找到元素后元素的宽高必须大于0才执行
     * @throws InterruptedException
     */
    public boolean elementVisualWaitHandle(WebDriver driver, By elementBy, SeleniumUtilInterface handle) throws InterruptedException {
        try {
            WebDriverWait wait = new WebDriverWait(driver, elementWaitTime);
            WebElement element = wait.until(ExpectedConditions.elementToBeClickable(elementBy));
            if (handle != null) {
                handle.action(element);
            }
            return true;
        } catch (NoSuchElementException e) {
            e.fillInStackTrace();
            return false;
        } catch (TimeoutException e) {
            e.fillInStackTrace();
            return false;
        } catch (NullPointerException e) {
            e.fillInStackTrace();
            return false;
        }
    }

    public boolean elementIsExitBase(WebDriver driver, By elementBy){
        try {
            WebDriverWait wait = new WebDriverWait(driver, elementWaitTime);
            wait.until(ExpectedConditions.presenceOfElementLocated(elementBy));;
            return true;
        } catch (NoSuchElementException e) {
            e.fillInStackTrace();
            return false;
        } catch (Exception e) {
            e.fillInStackTrace();
            System.out.println(e.getClass().getName());
            return true;
        }
    }

    /**
     * 自定义超时时间 定位不到元素
     * @param driver
     * @param elementBy
     * @param time
     * @return
     */
    public boolean elementIsExitBaseByTime(WebDriver driver, By elementBy,int time){
        try {
            WebDriverWait wait = new WebDriverWait(driver, time);
            wait.until(ExpectedConditions.presenceOfElementLocated(elementBy));;
            return true;
        } catch (NoSuchElementException e) {
            e.fillInStackTrace();
            System.out.println("没有找到元素异常");
            return false;
        } catch (TimeoutException e){
            e.fillInStackTrace();
            return false;
        }
        catch (Exception e) {
            e.fillInStackTrace();
            System.out.println(e.getClass().getName());
            return true;
        }
    }


    public WebElement getElementBase(WebDriver driver, By elementBy) {
        WebDriverWait wait = new WebDriverWait(driver, elementWaitTime);
        WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(elementBy));
        return element;
    }

    public List<WebElement> getElementsBase(WebDriver driver, By elementBy) {
        WebDriverWait wait = new WebDriverWait(driver, elementWaitTime);
        List<WebElement> element = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(elementBy));
        return element;
    }


    public boolean elementWaitHandle(By elementBy, SeleniumUtilInterface handle) throws InterruptedException {
        try {
            WebDriverWait wait = new WebDriverWait(driver, elementWaitTime);
            WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(elementBy));
            if (handle != null) {
                handle.action(element);
            }
            return true;
        } catch (NoSuchElementException e) {
            e.fillInStackTrace();
            return false;
        } catch (TimeoutException e) {
            e.fillInStackTrace();
            return false;
        } catch (NullPointerException e) {
            e.fillInStackTrace();
            return false;
        }
    }
}
